﻿//-----------------------------------------------------------------------
// <copyright file="FilterSetSelectionListsCommand.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 
namespace ChatBox.UI.Windows.PrivateMessages
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using Commands;

    using DomainObjects.XmlFeedObjects;

    using Interfaces;

    using MessageFilters;

    using XamlUtility;

    /// <summary>
    /// Command to set the filter's selection lists
    /// </summary>
    public class FilterSetSelectionListsCommand : GenericCommandBase<FilterViewModel>
    {
        /// <summary>
        /// The chat engine
        /// TOOD: verify that this is the right instance. Singleton?
        /// </summary>
        private readonly IChatEngine chatEngine;

        /// <summary>
        /// Initializes a new instance of the FilterSetSelectionListsCommand class.
        /// </summary>
        /// <param name="chatEngine">
        /// The chat engine.
        /// </param>
        public FilterSetSelectionListsCommand(IChatEngine chatEngine)
        {
            this.chatEngine = chatEngine;
        }

        /// <summary>
        /// set up the selection lists
        /// </summary>
        /// <param name="value">the view model</param>
        public override void ExecuteOnValue(FilterViewModel value)
        {
            if (this.chatEngine.PrivateChatMessages != null)
            {
                SetGroupData(this.chatEngine.PrivateChatMessages, value);
                SetSenderData(this.chatEngine.PrivateChatMessages, value);
            }
        }

        /// <summary>
        /// Populate the "sender" data
        /// </summary>
        /// <param name="messages">the messages to inspect</param>
        /// <param name="viewModel">the view model</param>
        private static void SetSenderData(IList<Message> messages, FilterViewModel viewModel)
        {
            int oldIndex = viewModel.SelectedSenderIndex;

            IList<Link> newFromLinks = messages.ListFrom();
            newFromLinks = newFromLinks.OrderBy(l => l.Text).ToList();

            viewModel.Senders.Clear();
            viewModel.Senders.Add(new Link { NodeId = 0, Text = " All senders" });
            viewModel.Senders.AddList(newFromLinks);

            viewModel.SelectedSenderIndex = oldIndex;
        }

        /// <summary>
        /// Populate the "group" data
        /// </summary>
        /// <param name="messages">the messages to inspect</param>
        /// <param name="viewModel">the view model</param>
        private static void SetGroupData(IList<Message> messages, FilterViewModel viewModel)
        {
            int oldIndex = viewModel.SelectedGroupIndex;

            IList<Link> newGroupLinks = messages.ListGroups();
            newGroupLinks = newGroupLinks.OrderBy(l => l.Text).ToList();

            viewModel.Groups.Clear();

            // add the default modes
            viewModel.Groups.Add(new GroupFilterSelection(GroupFilterType.ShowAllMessages));
            viewModel.Groups.Add(new GroupFilterSelection(GroupFilterType.ShowAllMessagesNotToAnyGroup));
            viewModel.Groups.Add(new GroupFilterSelection(GroupFilterType.ShowAllMessagesToAnyGroup));

            // add a record for each group
            viewModel.Groups.AddList(newGroupLinks.Select(gl => new GroupFilterSelection(gl)));

            viewModel.SelectedGroupIndex = oldIndex;
        }
    }
}
